#!/bin/bash

read -p "请问需要部署哪个系统的PXE环境？(6.4/6.5/6.8/7.2/7.3)" answer

ans=`echo $answer | sed "s/\./u/g"`

lan_dev=`/sbin/ifconfig | awk '(/eth/ || /em[0-9]/) {print $1}' | head -n 1`
lan_ip=`/sbin/ifconfig $lan_dev | grep "inet addr" | awk '{print$2}' | cut -d: -f2`
prefix=`echo $lan_ip | cut -d'.' -f1,2,3`
#wget tftpboot
wget -O /tmp/tftpboot.tgz http://test.wgmf.com/mirrors/pxe/tftpboot.tgz
tar xvf /tmp/tftpboot.tgz -C /
sed -i "s/10.33.9.250/${lan_ip}/g" /tftpboot/pxelinux.cfg/default

#install tftp-server
#rpm -qa | grep tftp-server || yum install -y tftp-server
yum install -y tftp-server
sed -i 's|/var/lib/tftpboot|/tftpboot|' /etc/xinetd.d/tftp 
sed -i "s/disable.*yes$/disable                 = no/" /etc/xinetd.d/tftp
chattr -i /etc/hosts.allow
sed -i "\$a all:${prefix}.0/255.255.255.0" /etc/hosts.allow

/etc/init.d/xinetd restart
if [ `netstat -tunlp | grep 69 | wc -l` -lt 1 ]
then 
    echo "xinetd 重启失败，请检查" | tee -a /tmp/pxe.log
    sleep 5
fi


#install dhcp
#rpm -qa | grep dhcp | grep -v dhcp-common || yum install -y dhcp
yum install -y dhcp

echo "ddns-update-style interim;
allow booting;
allow bootp;
next-server $lan_ip;
filename \"pxelinux.0\";
ignore client-updates;
set vendorclass = option vendor-class-identifier;
option pxe-system-type code 93 = unsigned integer 16;
subnet ${prefix}.0 netmask 255.255.255.0 {
option routers $lan_ip;
option subnet-mask 255.255.255.0;
range dynamic-bootp ${prefix}.100 ${prefix}.254;
default-lease-time 21600;
max-lease-time 43200;
}
# group for Cobbler DHCP tag: default
group {
}" >/etc/dhcp/dhcpd.conf

/etc/init.d/dhcpd start

if [ `netstat -tunlp | grep dhcpd | wc -l` -lt 1 ]
then 
    echo "dhcp 重启失败，请检查" | tee -a /tmp/pxe.log
    sleep 5
fi

#download iso 
mkdir -p /data/iso
chmod 755 /data
chmod 755 /data/iso

cd /data/iso
#/bin/echo $answer | grep 6.5 && wget CentOS-6.5-x86_64-bin-DVD1.iso && wget ks/6u5_lan.cfg && mkdir CentOS-6.5-x86_64 && mount -o loop -t iso9660 /data/iso/CentOS-6.5-x86_64-bin-DVD1.iso /data/iso/CentOS-6.5-x86_64
#/bin/echo $answer | grep 6.8 && wget /ks/CentOS-6.8-x86_64-bin-DVD1.iso && wget /ks/6u8_lan.cfg && mkdir CentOS-6.8-x86_64 && mount -o loop -t iso9660 /data/iso/CentOS-6.8-x86_64-bin-DVD1.iso /data/iso/CentOS-6.8-x86_64
#/bin/echo $answer | grep 7.2 && wget /CentOS-7-x86_64-DVD-1511.iso && wget /ks/7u2_lan.cfg && mkdir CentOS-7.2-x86_64 && mount -o loop -t iso9660 /data/iso/CentOS-7-x86_64-DVD-1511.iso /data/iso/CentOS-7.2-x86_64
#/bin/echo $answer | grep 7.3 && wget /ks/CentOS-7-x86_64-DVD-1611.iso && wget /ks/7u3_lan.cfg && mkdir CentOS-7.3-x86_64 && mount -o loop -t iso9660 /data/iso/CentOS-7-x86_64-DVD-1611.iso /data/iso/CentOS-7.3-x86_64

#echo "/data/iso/CentOS-${answer}-x86_64" >> /usr/local/monitor-base/log/disk.deny 
sed -i "s/10.33.9.250/${lan_ip}/g" /data/iso/${ans}_lan.cfg
chmod 755 /data/iso/${ans}_lan.cfg

#nginx
if [ `netstat -tunlp | grep 3195 | wc -l` -lt 1 ]
then
    cd /tmp
    wget http://test.wgmf.com/mirrors/pxe/gsx.tgz
    tar -zxvf gsx.tgz -C /usr/local/
    sed -i "s/10.1.1.250/$lan_ip/" /usr/local/gsx/nginx/conf/nginx.conf
    
    /usr/local/gsx/nginx/sbin/nginx
    
    if [ `netstat -tunlp | grep 3195 | wc -l` -lt 1 ]
    then
        echo "nginx 重启失败，请检查" | tee -a /tmp/pxe.log
        sleep 5
    fi
else
    echo "nginx已经安装" | tee -a /tmp/pxe.log
fi

if [ -f /tmp/pxe.log ]
then
    echo "PXE安装有问题"
    cat /tmp/pxe.log
else
    echo "PXE环境部署完成
请记住，安装完系统，关闭dhcp服务，切记！"
fi
#rm ~/pxe.sh
